package com.ks.component.sms;

import com.ks.common.util.CommonUtil;
import com.ks.config.SmsConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 短信组件
 *
 * @author KingAusten
 * @date 2022/08/18 15:38:58
 */
@Slf4j
@Component
@AllArgsConstructor
public class SmsComponent{

    /**
     * 发送地址
     */
    private static final String URL_TEMPLATE = "https://jmsms.market.alicloudapi.com/sms/send?mobile=%s&templateId=%s&value=%s";

    private RestTemplate restTemplate;

    private SmsConfig smsConfig;

    /**
     * 发送短信验证码
     *
     * @param to
     * @param templateId
     * @param value
     */
    @Async("threadPoolTaskExecutor")
    public void send(String to,String templateId,String value){

        long beginTime = CommonUtil.getCurrentTimestamp();

        String url = String.format(URL_TEMPLATE,to,templateId,value);
        HttpHeaders headers = new HttpHeaders();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.set("Authorization","APPCODE " + smsConfig.getAppCode());
        HttpEntity entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.POST,entity,String.class);

        long endTime = CommonUtil.getCurrentTimestamp();

        log.info("耗时={},url={},body={}",endTime - beginTime,url,response.getBody());
        if(response.getStatusCode()
                .is2xxSuccessful()){
            log.info("发送短信验证码成功:{}",response.getBody());
        }else{
            log.error("发送短信验证码失败:{}",response.getBody());
        }

    }

}
